package com.cskaoyan.service.wx.sl;

import com.cskaoyan.bean.wx.sl.WxComment;
import com.cskaoyan.bean.wx.sl.WxCommentExample;
import com.cskaoyan.mapper.WxCommentMapper;
import com.cskaoyan.vo.wx.sl.WxCommentCountVo;
import com.cskaoyan.vo.wx.sl.WxCommentDataVo;
import com.cskaoyan.vo.wx.sl.WxCommentListVo;
import com.cskaoyan.vo.wx.sl.WxCommentUserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    WxCommentMapper wxCommentMapper;

    //显示评论列表
    @Override
    public WxCommentListVo commentList(Integer valueId, byte type, Integer size, Integer page, Integer showType) {
        //分页排序
        PageHelper.startPage(page, size);
        //先筛选type的,0是商品评论，1是专题评论
        WxCommentExample wxCommentExample = new WxCommentExample();
        WxCommentExample.Criteria criteria = wxCommentExample.createCriteria();
        criteria.andTypeEqualTo(type);
        List<WxComment> wxComments = wxCommentMapper.selectByExample(wxCommentExample);

        PageInfo<WxComment> goodsPageInfo = new PageInfo<>(wxComments);//查询结果放进来
        long total = goodsPageInfo.getTotal();//总的数量select count(*)
        //写死评论用户
        WxCommentUserVo userVo = new WxCommentUserVo();
        userVo.setNickName("干柿鬼鲛");
        userVo.setAvatarUrl("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1251494581,1620677899&fm=26&gp=0.jpg");
        //总，取出List<WxCommentDataVo> data
        WxCommentListVo wxCommentListVo = new WxCommentListVo();
        List<WxCommentDataVo> ssss = new ArrayList<>();
        //封装DataVo
        WxCommentDataVo wxCommentDataVo = new WxCommentDataVo();
        List<String> piclist = new ArrayList<>();
        for (WxComment comment : wxComments) {
            Date addTime = comment.getAddTime();
            String content = comment.getContent();
            String[] picUrls = comment.getPicUrls();
            for (String picUrl : picUrls) {
                piclist.add(picUrl);
            }
            WxCommentDataVo dataVo = new WxCommentDataVo(userVo,addTime,piclist,content);
            ssss.add(dataVo);
        }
        //封装
        WxCommentListVo listVo = new WxCommentListVo(total, page, ssss);
        return listVo;
    }
    //显示评论总数、和有图片评论的数量
    @Override
    public WxCommentCountVo commentCount(Integer valueId, byte type) {
        WxCommentExample wxCommentExample = new WxCommentExample();
        WxCommentExample.Criteria criteria = wxCommentExample.createCriteria();
        criteria.andTypeEqualTo(type);
        List<WxComment> wxComments = wxCommentMapper.selectByExample(wxCommentExample);
        //迭代
        int count = 0; //计算总数
        int num = 0;  //带图的总数
        boolean str = true;
        for (WxComment wxComment : wxComments) {
            Integer countValueId = wxComment.getValueId();
            Boolean hasPicture = wxComment.getHasPicture();
            if (countValueId.equals(valueId)) {
                if (hasPicture.equals(str)) {
                    num++;
                }
                    count++;
            }
        }
        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo(num,count);
        return wxCommentCountVo;
    }
}
